package socket;

import java.io.IOException;
import java.net.*;

/**
 * Created by zzy on 2018/1/9.
 */
public class UDPClient {

    public static void main(String[] args) throws IOException {
        /**
         * 发送
         */
        //1、指定服务器响应信息
        InetAddress address=InetAddress.getByName("localhost");
        int port=9999;
        byte[] data="zzy,123".getBytes();
        //2、创建数据包
        DatagramPacket packet=new DatagramPacket(data,data.length,address,port);
        //3、创建DatagramSocket
        DatagramSocket socket=new DatagramSocket();
        //4、发送
        socket.send(packet);
        /**
         * 接收
         */
        //1、创建数据包
        byte[] getData=new byte[1024];
        DatagramPacket packet1=new DatagramPacket(getData,getData.length);
        socket.receive(packet1);
        //3、读取
        String reply=new String(getData,0,packet1.getLength());
       Print.print("客户端，服务器说"+reply);
       socket.close();
    }
}
